package com.lin.musicplayer.Interface;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.lin.musicplayer.MusicFilter;
import com.lin.musicplayer.MusicPlay;
import com.lin.musicplayer.R;
import com.lin.musicplayer.db.MusicDB;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by crystal on 2016/7/7.
 */
public class Love extends Activity {

    private SQLiteDatabase dbRead,dbWrite;
    private MusicDB db;
    private static final File Music_Path=new File("/sdcard/mp3/");
    public List<String> musiclist;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.love);
        db = new MusicDB(this);
        db.setName("love");
        dbRead = db.getReadableDatabase();
        dbWrite = db.getWritableDatabase();
        Love();
        listView=(ListView)findViewById(R.id.lovelist);
        setListAdapter();
        listView.setOnItemClickListener(new ItemClickEvent());
       // saveMusic();
       // musicDelete();
    }
    public void Love()
    {
        musiclist=new ArrayList<String>();
        Cursor cursor = dbRead.query("love",new String[]{"name"},null,null,null,null,null);
        System.out.println("my love music list\n");
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            name="/sdcard/mp3/"+name;
            File file=new File(name);
            musiclist.add(file.getAbsolutePath());
            System.out.printf("%s\n",name);
        }
    }
    public void setListAdapter()
    {
        List<Map<String,Object>> date=new ArrayList<Map<String,Object>>();
        for(String path:musiclist)
        {
            Map<String,Object>map=new HashMap<String, Object>();
            File file=new File(path);
            map.put("fileName",file.getName());
            date.add(map);
            SimpleAdapter adapter=new SimpleAdapter(this,date,android.R.layout.simple_list_item_2,new String[]{"fileName"},new int[]{android.R.id.text1});
            listView.setAdapter(adapter);
        }
    }
    private final class ItemClickEvent implements AdapterView.OnItemClickListener {

        Map<String, Object> map = new HashMap<String, Object>();

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            map = (Map<String, Object>) listView.getItemAtPosition(i);
            String text = (String) map.get("fileName");
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("fileName", text);
            intent.putExtras(bundle);
            intent.setClass(Love.this, MusicPlay.class);
            startActivity(intent);
        }

        public int saveMusic() {
            ContentValues cv = new ContentValues();
            cv.put("name", "Various.mp3");
            return (int) dbWrite.insert("love", null, cv);
        }
    }
    public void musicDelete()
    {
       int result=dbWrite.delete("love","name like?",new String[]{"xiyaoshan.mp3"});
        System.out.printf("the result=%d\n",result);
    }
    @Override
    protected void onDestroy() {
        dbRead.close();
        dbWrite.close();
        super.onDestroy();
    }
}
